public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        if(root == null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = root;
        while (cur != null) {
            if(key < cur.key) {  //比节点小插到左树
                parent = cur;
                cur = cur.left;
            } else if(key > cur.key) { //比节点大插到右树
                parent = cur;
                cur = cur.right;
            } else {
                return false;   //表示已存在这个数，不用再插入
            }
        }
        if(parent.key > key) {
            parent.left = new TreeNode(key);
        } else {
            parent.right = new TreeNode(key);
        }

        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if(key < cur.key) {  //比节点小走到左树
                cur = cur.left;
            } else if(key > cur.key) { //比节点大走到右树
                cur = cur.right;
            } else {
                return cur;   //表示找到这个数
            }
        }
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if(key < cur.key) {  //比节点小走到左树
                parent = cur;
                cur = cur.left;
            } else if(key > cur.key) { //比节点大走到右树
                parent = cur;
                cur = cur.right;
            } else {
                removeNode(parent,cur); //删除节点
                return true;
            }
        }
        return false;
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {  //删除节点的左树为空
            if(cur == root) {   //要删除的节点是根节点
                root = root.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else if(cur == parent.right) {
                parent.right = cur.left;
            }
        }
        else if (cur.right == null) { //删除节点的右树为空
            if(cur == root) {   //要删除的节点是根节点
                root = root.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else if(cur == parent.right) {
                parent.right = cur.right;
            }
        }
        else{ //删除节点左右子树都不为空
            //找cur右子树的最左节点（找替罪羊）
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.key = target.key; //将替罪羊赋值给要删除的节点cur
            if(targetParent.left == target) {
                targetParent.left = target.right; //删除替罪羊
            }
            else {  //target 没有左子树
                cur.right = target.right;
            }

        }
    }

}
